<template>
    <view>
        <start-end-date-time @confirm="handleDateTimeConfirm" :defaultStartDate="defaultStartDate"
            :defaultStartTime="defaultStartTime" :defaultEndDate="defaultEndDate" :defaultEndTime="defaultEndTime" />
    </view>
</template>

<script>
    import startEndDateTime from '@/uni_modules/start-end-date-time/components/start-end-date-time/start-end-date-time.vue';
    export default {
        components: {
            startEndDateTime
        },
        data() {
            return {
                defaultStartDate: '',
                defaultStartTime: '',
                defaultEndDate: '',
                defaultEndTime: ''
            }
        },
        onLoad() {
            // 页面加载时获取存储的日期时间数据
            const dateTimeData = JSON.parse(uni.getStorageSync('selectedDateTime'))
            if (dateTimeData) {
                this.defaultStartDate = dateTimeData.start.date
                this.defaultStartTime = dateTimeData.start.time
                this.defaultEndDate = dateTimeData.end.date
                this.defaultEndTime = dateTimeData.end.time
            } else {
                // 如果没有存储数据，设置默认值为明天和后天
                const tomorrow = new Date()
                tomorrow.setDate(tomorrow.getDate() + 1)
                const dayAfterTomorrow = new Date()
                dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2)

                // 格式化日期为 YYYY-MM-DD
                const formatDate = (date) => {
                    const year = date.getFullYear()
                    const month = String(date.getMonth() + 1).padStart(2, '0')
                    const day = String(date.getDate()).padStart(2, '0')
                    return `${year}-${month}-${day}`
                }

                this.defaultStartDate = formatDate(tomorrow)
                this.defaultStartTime = '09:00'
                this.defaultEndDate = formatDate(dayAfterTomorrow)
                this.defaultEndTime = '09:00'
            }
        },
        methods: {
            handleDateTimeConfirm(result) {
				console.log('qweqe')
                // 保存选择的日期时间到本地存储
                // uni.setStorageSync('selectedDateTime', result)
				 this.$storage.setJson("selectedDateTime",result)
                uni.navigateBack(-1)
            }
        }
    }
</script>